import * as THREE from "three";
import OrbitControls from "./three/OrbitControls";
import toolFlushStats from "@/tool/tool-flush-stats";

let renderer;
let scene;
let camera;
/**
 * 初始化默认场景
 */
function initRender(width, height, _scene, _camera, isMouse = true) {
  scene = _scene;
  camera = _camera;

  // 创建渲染器对象
  renderer = new THREE.WebGLRenderer({
    antialias: true, // 抗锯齿
  });
  // 设置渲染器尺寸大小
  renderer.setSize(width, height);

  animate();

  if (isMouse) {
    initMouse();
  }

  return renderer.domElement;
}

/**
 * 渲染函数
 */
function animate() {
  requestAnimationFrame(animate);
  toolFlushStats.flush();
  renderer.render(scene, camera);
}

function initMouse() {
  var controls = new OrbitControls(camera, renderer.domElement); //创建控件对象
  controls.addEventListener("change", () => renderer.render(scene, camera)); //监听鼠标、键盘事件
}

export default {
  initRender,
  animate,
  initMouse,
};
